package department

import (
	"context"
	"fmt"
	"net/http"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListLogic) List() (resp *types.DepartmentListResponse, err error) {
	resp = new(types.DepartmentListResponse)

	//1.查询所有部门
	departments, err := l.svcCtx.DepartmentModel.FindByPage(l.ctx, "")
	if err != nil {
		fmt.Printf("[Error]查询所有部门:%s\n", err.Error())
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	//2.
	resp.Data = make([]types.Department, 0)
	for _, department := range departments {
		resp.Data = append(resp.Data, types.Department{
			Id:        department.Id,
			Type:      department.Type,
			SortId:    department.SortId,
			ParentId:  department.ParentId,
			Name:      department.Name,
			FullName:  department.FullName,
			Code:      department.Code,
			Remark:    department.Remark,
			CreatedAt: department.CreatedAt.Unix(),
			UpdatedAt: department.UpdatedAt.Unix(),
		})
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"

	return resp, nil
}
